/**
 * 
 */
package de.cbf.cam.dsa.model.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import de.cbf.cam.dsa.model.entities.WeaponStyle;
import de.cbf.cam.dsa.model.entities.items.Item;

/**
 * @author Nebelritter
 *
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class BaseWeapon extends Item {
	public static String UNARMED ="unbewaffnet";
	
	/**
	 * @uml.property  name="weaponType"
	 */
	protected String weaponType;	
	/**
	 * @uml.property  name="styles"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.persistence.Header"
	 */
	protected List<WeaponStyle> styles 		= new ArrayList<WeaponStyle>();
	/**
	 * bitwise specials from WeaponSpecialConstants
	 * @uml.property  name="specialStates"
	 */
	protected int specialStates;  	
	
	/**
	 * @uml.property  name="price"
	 */
	protected int price;
	
	/**
	 * @uml.property  name="tpDiceNumber"
	 */
	protected int tpDiceNumber;
	/**
	 * @uml.property  name="tpDiceSides"
	 */
	protected int tpDiceSides;
	/**
	 * @uml.property  name="tpBonus"
	 */
	protected int tpBonus;
	
	/**
	 * 
	 */
	protected BaseWeapon() {
		super();
		init();
	}
	/**
	 * method to initialize object,
	 * provide default values for every field
	 * accessable by all constructors
	 */
	private void init() {	
		name			= "";
		weaponType 		= "";
		price			= 0;
		tpDiceNumber 	= 0;
		tpDiceSides		= 0;
		tpBonus			= 0;
	}
	
	@Transient
	public String getWeaponTPString() {
		// get TP string of current Weapon "2W+10"		
		StringBuffer buf= new StringBuffer(10);
		buf.append(tpDiceNumber);
		buf.append("W");
		buf.append(tpDiceSides);
		buf.append("+");
		buf.append(tpBonus);
		return buf.toString();
	}
	
	
	//========================
//				getters
	//========================
		/**
		 * @return the name
		 */
		public String getName() {
			if("".equals(name)){
				return weaponType;
			}
			return name+"("+weaponType+")";
		}
		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			this.name = name;
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"name",null,name);
		}
		@Transient
		public String getTpString(){
			// get TP string of current Weapon "2W+10"		
			StringBuffer buf= new StringBuffer(10);
			buf.append(tpDiceNumber);
			buf.append("W");
			buf.append(tpDiceSides);
			buf.append("+");
			buf.append(tpBonus);
			return buf.toString();
		}
		
		
		/**
		 * @return the styles
		 */
		@ManyToMany(cascade= CascadeType.ALL)
		public List<WeaponStyle> getStyles() {
			return styles;
		}
		
		public boolean addStyle(WeaponStyle style){
			boolean result = this.styles.add(style);
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"styles",null,styles);
			return result;
			
		}
		public boolean removeStyle(WeaponStyle style){
			boolean result = this.styles.remove(style);
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"styles",null,styles);
			return result;
		}	
		public void setStyles(List<WeaponStyle> styles){
			this.styles = styles;
		}
		/**
		 * @return  the tpDiceNumber
		 * @uml.property  name="tpDiceNumber"
		 */
		public int getTpDiceNumber() {
			return tpDiceNumber;
		}
		/**
		 * @param tpDiceNumber  the tpDiceNumber to set
		 * @uml.property  name="tpDiceNumber"
		 */
		public void setTpDiceNumber(int tpDiceNumber) {
			this.tpDiceNumber = tpDiceNumber;
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpDiceNumber",null,tpDiceNumber);
		}
		/**
		 * @return  the tpDiceSides
		 * @uml.property  name="tpDiceSides"
		 */
		public int getTpDiceSides() {
			return tpDiceSides;
		}
		/**
		 * @param tpDiceSides  the tpDiceSides to set
		 * @uml.property  name="tpDiceSides"
		 */
		public void setTpDiceSides(int tpDiceSides) {
			this.tpDiceSides = tpDiceSides;
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpDiceSides",null,tpDiceSides);
		}
		/**
		 * @return  the tpBonus
		 * @uml.property  name="tpBonus"
		 */
		public int getTpBonus() {
			return tpBonus;
		}

		/**
		 * @param tpBonus  the tpBonus to set
		 * @uml.property  name="tpBonus"
		 */
		public void setTpBonus(int tpBonus) {
			this.tpBonus = tpBonus;
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpBonus",null,tpBonus);
		}
		
		/**
		 * @return  the price
		 * @uml.property  name="price"
		 */
		public int getPrice() {
			return price;
		}

		
		/**
		 * @return
		 * @uml.property  name="weaponType"
		 */
		public String getWeaponType() {
			return weaponType;
		}
	//========================
//					setters
	//========================
		

		
		

		/**
		 * @param price  the price to set
		 * @uml.property  name="price"
		 */
		public void setPrice(int price) {
			this.price = price;
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"pice",null,price);
		}
		/**
		 * @param type
		 * @uml.property  name="weaponType"
		 */
		public void setWeaponType(String type) {
			this.weaponType = type;
		}
		/**
		 * @return
		 * @uml.property  name="specialStates"
		 */
		public int getSpecialStates() {
			return specialStates;
		}
		public void addSpecialState(int state){
			specialStates |= state;
		}
		/**
		 * @param specialStates
		 * @uml.property  name="specialStates"
		 */
		public void setSpecialStates(int specialStates) {
			this.specialStates = specialStates;
		}
}
